package cn.changeforyou.demo.netty.common;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhyu
 * @date 2021/3/18 9:01
 */
public class YbkjWebsocketDataDecodeHandler extends ChannelInboundHandlerAdapter {

    Logger log = LoggerFactory.getLogger(YbkjWebsocketDataDecodeHandler.class);

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        TextWebSocketFrame frame = (TextWebSocketFrame) msg;
        String text = frame.text();
        Data data = JsonUtils.toEntity(text, Data.class);
        String contentStr = JsonUtils.toJson(data.getContent());
        switch (data.getMsgType()){
            case req:
                data.setContent(JsonUtils.toEntity(contentStr, Req.class));
                break;
            case heart:
                data.setContent(contentStr);
                break;
        }
        super.channelRead(ctx, data);
    }

}
